package com.wanli.agent.homepage;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.wanli.agent.R;
import com.wanli.agent.base.BaseActivity;
import com.wanli.agent.base.DataCallBack;
import com.wanli.agent.bean.BankCardBean;
import com.wanli.agent.bean.IdCardBean;
import com.wanli.agent.bean.ImageBean;
import com.wanli.agent.bean.UserInfoBean;
import com.wanli.agent.dialog.UpdateSettlementDialog;
import com.wanli.agent.homepage.model.HomePageModelImpl;
import com.wanli.agent.homepage.model.IHomePageModel;
import com.wanli.agent.mine.model.MineModelImpl;
import com.wanli.agent.utils.Base64BitmapUtil;
import com.wanli.agent.utils.DateTimeUtil;
import com.wanli.agent.utils.FastClickUtils;
import com.wanli.agent.utils.SPManager;
import com.wanli.agent.utils.SPUtils;
import com.wanli.agent.utils.ToastUtil;
import com.wanli.agent.widget.AppTitleBar;
import com.wanli.agent.widget.ViewLoading;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettlementInfoActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_settle_name)
    TextView tvSettleName;
    @BindView(R.id.tv_settle_mobile)
    TextView tvSettleMobile;
    @BindView(R.id.tv_id_card)
    TextView tvIdCard;
    @BindView(R.id.tv_card_no)
    TextView tvCardNo;
    @BindView(R.id.tv_card_name)
    TextView tv_card_name;
    @BindView(R.id.tv_bank_city)
    TextView tv_bank_city;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.tv_settlement_account_type)
    TextView tv_settlement_account_type;
    private UserInfoBean userInfoBean;
    private UpdateSettlementDialog dialog;
    private String document_type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settlement_info);
        homePageModel = new HomePageModelImpl();
        ButterKnife.bind(this);
        requestUserInfo();
    }

    public void requestUserInfo() {
        new MineModelImpl().requestMine(new DataCallBack<UserInfoBean>() {
            @Override
            public void onSuccessful(UserInfoBean bean) {
                userInfoBean = bean;
                SPManager.getInstance().saveUserInfoJson(new Gson().toJson(bean));
                tvSettleName.setText(bean.getData().getSettle_name());
                tvSettleMobile.setText(bean.getData().getSettle_mobile());
                tvIdCard.setText(bean.getData().getId_card());
                tvCardNo.setText(bean.getData().getCard_no());
                tv_card_name.setText(bean.getData().getBank_name());
                tv_bank_city.setText(bean.getData().getBank_city());
                String settle_type = bean.getData().getSettle_type();

                if ("corporate".equals(settle_type)){
                    tv_settlement_account_type.setText("对公账户");
                }else if ("personal".equals(settle_type)){
                    tv_settlement_account_type.setText("对私账户");
                }

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }


    @OnClick(R.id.tv_commit)
    public void onViewClicked() {
        if (FastClickUtils.isFastClick()) {
            return;
        }
        if (userInfoBean == null) {
            return;
        }
        dialog = UpdateSettlementDialog.newInstance(userInfoBean.getData().getSettle_name(), userInfoBean.getData().getSettle_mobile(), userInfoBean.getData().getId_card(), userInfoBean.getData().getCard_no(),userInfoBean.getData().getBank_name(),userInfoBean.getData().getBank_city(),userInfoBean.getData().getSettle_type());
        dialog.show(getSupportFragmentManager(), "dialog");
        dialog.setOnCallBackListener(new UpdateSettlementDialog.OnCallBackListener() {
            @Override
            public void onCallBack(String settleName, String settleMobile, String idCard, String cardNo,String bank_name,String bankType,String bank_city) {
                requestUserInfo();
            }
        });
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //1 银行卡 2 身份证
        document_type = SPUtils.get(SPManager.Key.DOCUMENT_TYPE, "0");


        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> mediaList = PictureSelector.obtainMultipleResult(data);
                    if (mediaList.size() > 0) {
                        String imgPath = mediaList.get(0).getCompressPath();
                        requestUploadFile(imgPath);
                    }
                    break;
            }
        }

    }
    /**
     * 上传图片
     */
    public void requestUploadFile(String imgPath) {

        ViewLoading.showProgress(mActivity, "上传中......");
        Bitmap bitmap = BitmapFactory.decodeFile(imgPath);
        String file = Base64BitmapUtil.bitmapToBase64(bitmap);
        homePageModel.requestUploadFile("store", file, new DataCallBack<ImageBean>() {
            @Override
            public void onSuccessful(ImageBean bean) {
                if (isDestroy()) {
                    return;
                }
                ToastUtil.showShort("上传成功");

                if ("1".equals(document_type)){
                    requestAliBankCard(bean.getData());
                }else if ("2".equals(document_type)){
                    requestAliIdCard(bean.getData(), "face");
                }

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestAliIdCard(String imgPath, String side) {
        ViewLoading.showProgress(mActivity, "识别中......");

        homePageModel.requestAliIdCard(imgPath, side, new DataCallBack<IdCardBean>() {
            @Override
            public void onSuccessful(IdCardBean bean) {
                if (bean.getData() != null && bean.getData().isSuccess()) {
                    ToastUtil.showShort("识别成功");
                    if ("face".equals(side)) {
                        dialog.getId(bean.getData().getName(),bean.getData().getNum());
                    }

                } else {
                    ToastUtil.showShort("识别失败，请输入");
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });

    }
    private IHomePageModel homePageModel;
    public void requestAliBankCard(String imgPath) {
        homePageModel.requestAliBankCard(imgPath, new DataCallBack<BankCardBean>() {
            @Override
            public void onSuccessful(BankCardBean bean) {
                if (bean.getData() != null && bean.getData().isSuccess()) {
                    ToastUtil.showShort("识别成功");
                    tvCardNo.setText(bean.getData().getCard_num());
                    dialog.get(bean.getData().getCard_num());

                } else {
                    ToastUtil.showShort("识别失败，请输入");
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }
}
